<?php

class LogDao extends Eloquent {

    function __construct() {
        parent::__construct();
        $this->table = 'log';
    }
    
    public function insertObject($obj){
        $result = DB::insert(DB::raw("INSERT INTO log (name, record_id, module_id, status_id, note, created_by, created_at) ".
            "VALUES ('".$obj->getName()."' ".
                     ", '".$obj->getRecordId()."' ".
                    ", '".$obj->getModuleId()."' ".
                    ", '".$obj->getStatusId()."' ".
                    ", '".$obj->getNote()."' ".
                    ", '".$obj->getCreatedBy()->getId()."' ".
                    ", null ".
                    ")"));

        if (!$result) { return null; }

        return $obj->getId();
    }

    public function updateObject($obj, $old_id){
       DB::select(DB::raw("UPDATE status SET "
               . "name = '".$obj->getName()."', "
               . "record_id = '".$obj->getRecordId()."', "
               . "module_id = '".$obj->getModuleId()."', "
               . "status_id = '".$obj->getStatusId()."', "
               . "note = '".$obj->getNote()."', "
               . "updated_at = null "
               . "Where id='".$old_id."'"));

       return true;
    }
    
    public function deleteObject($id){
        DB::delete(DB::raw("DELETE FROM status where id='".$id."'"));
        
        return true;
    }
    
    public function getObject($id){
        $result = DB::select(DB::raw("SELECT * FROM status where id='".$id."'"));

        if (!$result) { return null; }
        return $this->toObject($result[0]);
    }
    
    public function getList($criteria = NULL){
        $result = DB::select(DB::raw("SELECT * FROM status"));
        $list = array();
        foreach ($result as $rowdata){
            $list[] = $this->toObject($rowdata);
        }
        return $list;
    }
    
    public function getListCount($criteria=null) {
        return parent::getListCount($criteria);
    }
    
    public function toObject($rowdata=null){
        $obj = new Log();
        $obj->setId($rowdata->id);
        $obj->setName($rowdata->name);
        $obj->setRecordId($rowdata->record_id);
        $obj->setModuleId($rowdata->module_id);
        $obj->setStatusId($rowdata->status_id);
        $obj->setNote($rowdata->note);
        $obj->setCreated($rowdata->created_by);
        
        return $obj;
    }
}
